from brian import *
from brian.library.random_processes import *
from brian.library.synapses import  *
import MoNeuron as Moto
import InNeuron as IN
from time import clock
from MyConstants import gSynE, gSynI
from matplotlib.backends.backend_pdf import PdfPages
from Rybak import *


use_delay=True


class human(rybak):
    def __init__(self):
        self.stype = 'alpha'
        super(human,self).__init__(0,True)
                
    def setup_connections(self):
        self.inputfreq = 30 * Hz
        
        self.spiketimes = []
        for i in xrange(0,20):
            self.spiketimes.append((i,15*ms+randn()*2*ms))
        
        #self.spiketimes.sort(key=lambda tup: tup[1])
        

        
        self.AfferentInput = SpikeGeneratorGroup(20,self.spiketimes,period=1/self.inputfreq)
        
        self.CIN_RGE=Connection(self.AfferentInput,self.RG_E,'ge',weight=gSynE*0.08,sparseness=1)
        self.CIN_RGF=Connection(self.AfferentInput,self.RG_F,'ge',weight=gSynE*0.078,sparseness=1)
        
        self.CIN_PFE=Connection(self.AfferentInput,self.PF_E,'ge',weight=gSynE*0.08,sparseness=1)
        self.CIN_PFF=Connection(self.AfferentInput,self.PF_F,'ge',weight=gSynE*0.08,sparseness=1)
        
        self.CIN_MNE=Connection(self.AfferentInput,self.MN_E,'ge',weight=gSynE*0.4,sparseness=1)
        self.CIN_MNF=Connection(self.AfferentInput,self.MN_F,'ge',weight=gSynE*0.4,sparseness=1)
        
        self.net.add(self.AfferentInput,self.CIN_RGE,self.CIN_RGF,self.CIN_PFE,self.CIN_PFF,self.CIN_MNE,self.CIN_MNF)
        super(human,self).setup_connections(use_delay=use_delay) 
        
        
    def __call__(self,infrq=30*Hz,j=1.0,k=1.0,l=1.0,para=[],value=[],DUR=10*second,filename='run',display=False,report=0):
        self.inputfreq=infrq
        filename = filename + ' ' + str(infrq) + ' Hz, k = ' + str(k)
        self.AfferentInput.__init__(20,self.spiketimes,period=1/self.inputfreq)
        


        #disableing afferent input to motoneurons
        #self.CIN_MNE.__init__(self.AfferentInput,self.MN_E,'ge',weight=gSynE*0,sparseness=1)
        #self.CIN_MNF.__init__(self.AfferentInput,self.MN_F,'ge',weight=gSynE*0,sparseness=1)
        
        #changing excitator weight of rhythm generator to MN
        self.PF_E_MN_E.__init__(self.PF_E,self.MN_E,'ge',weight=gSynE*0.5*k,sparseness=1,structure='dense',max_delay=10*ms if use_delay else 0 *ms,delay= lambda i,j:7*ms+randn(21)*2*ms if use_delay else 0*ms);
        self.PF_F_MN_F.__init__(self.PF_F,self.MN_F,'ge',weight=gSynE*0.5*k,sparseness=1,structure='dense',max_delay=10*ms if use_delay else 0 *ms,delay= lambda i,j:7*ms+randn(21)*2*ms if use_delay else 0*ms);
       
        #inclreasing afferent input to motoneurons
        self.CIN_MNE.__init__(self.AfferentInput,self.MN_E,'ge',weight=gSynE*0.4*j,sparseness=1)
        self.CIN_MNF.__init__(self.AfferentInput,self.MN_F,'ge',weight=gSynE*0.4*j,sparseness=1)
        
        #increasing inhibition of mn from Ia
        self.IA_F_MN_E.__init__(self.IA_F,self.MN_E,'gi',weight=gSynI*0.6*l,sparseness=1,structure='dense',max_delay=10*ms if use_delay else 0 *ms,delay= lambda i,j:7*ms+randn(21)*2*ms if use_delay else 0*ms);
        self.IA_E_MN_F.__init__(self.IA_E,self.MN_F,'gi',weight=gSynI*0.6*l,sparseness=1,structure='dense',max_delay=10*ms if use_delay else 0 *ms,delay= lambda i,j:7*ms+randn(21)*2*ms if use_delay else 0*ms);
        
        
        super(human,self).__call__(para,value,DUR=DUR,report=report,display=display,filename=filename,run=False)
        #self.MN.dyn = 0

        self.INFactory.SG_RG.drive = 0
        self.INFactory.SG_PF.drive = 0
        #self.INFactory.SG_RG.GK = 1.1*msiemens*cm**-2
        
        self.inject_assignments(para,value)
        
        self.run()